
import validator from "/utils/validators";
import { moment, bus } from "/utils/core";
import { showToast } from "/utils/common";
import { payType } from "/utils/dictionary";
const app = getApp();

let arr = []
for (const key in payType) {
  arr.push({
    label: payType[key],
    value: key
  })
}

Component({
  data: {
    show: false,
    index: null,
    productList: [],
    payType,
    payTypes: arr,
    form: {
      contractId: 0,
      productId: null,
      productIndex: null,
      depositDate: null,
      amount: null,
      depositType: null,
      payTypeIndex: null,
      sysEnclosureList: []
    }
  },
  props: {
    slectedProducts: [], // 合同内产品
    products: [], // 所有产品
    productMap: {},
    depositList: [], // 收款记录
    onChange: () => { }
  },
  didMount() {
    bus.subscribe('firstDepositFile', (files) => {
      if (files.length > 0) {
        this.data.form.sysEnclosureList.push(...files)
        this.setData({
          "form.sysEnclosureList": this.data.form.sysEnclosureList
        });
      }
    })
  },
  didUpdate() { },
  didUnmount() {
    bus.remove("firstDepositFile")
  },
  methods: {
    modifyProductForm(e) {
      let form = JSON.parse(JSON.stringify(this.props.depositList[e.target.dataset.index]));
      this.setData({
        index: e.target.dataset.index,
        show: true
      });
      this.filterSelectableProductList();
      form.productIndex = this.data.productList.findIndex(item => item.value === form.productId);
      form.payTypeIndex = this.data.payTypes.findIndex(item => item.value === form.depositType);
      console.log(form, this.data.productList)
      this.setData({
        form: form
      });
    },
    onRemoveProduct(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.props.onChange({
              index: e.target.dataset.index,
              form: null
            });
          }
        },
      });
    },
    catchMethod() { },
    onFormSubmit(e) {
      Object.assign(this.data.form, e.detail.value);
      this.setData({
        "form.amount": parseFloat(this.data.form.amount)
      })
      let validate = new validator();
      validate.add("checkEmpty", this.data.form.productId, "请选择收款产品");
      validate.add("checkEmpty", this.data.form.depositDate, "收款日期不能为空");
      validate.add("checkIntNotZero", this.data.form.amount);
      validate.add("checkEmpty", this.data.form.depositType, "请选择收款方式");
      validate.add("checkEmptyArray", this.data.form.sysEnclosureList, "请上传收款凭证");
      if (validate.check()) {
        this.setData({
          show: false
        })
        this.props.onChange({
          index: this.data.index,
          form: this.data.form
        });
      }
    },
    showModal() {
      this.setData({
        show: true,
        index: null,
        form: {
          contractId: 0,
          productIndex: null,
          productId: null,
          depositDate: null,
          amount: null,
          depositType: null,
          payTypeIndex: null,
          sysEnclosureList: []
        }
      })
      this.filterSelectableProductList()
    },
    hideModal() {
      this.setData({
        show: false,
        index: null,
        form: {
          contractId: 0,
          productId: null,
          productIndex: null,
          depositDate: null,
          amount: null,
          depositType: null,
          payTypeIndex: null,
          sysEnclosureList: []
        }
      })
    },
    showDatePicker(e) {
      dd.datePicker({
        format: 'yyyy-MM-dd',
        currentDate: moment().format('YYYY-MM-DD'),
        success: (res) => {
          if (res.date) {
            this.setData({
              "form.depositDate": res.date
            })
          }
        },
      });
    },
    filterSelectableProductList() {
      let arr = []
      this.props.slectedProducts.forEach(product => {
        arr.push({
          label: this.props.products.find(item => item.id === product.productId).name,
          value: product.productId
        })
      });
      this.setData({
        productList: arr
      });
    },
    onProductChange(e) {
      this.setData({
        "form.productIndex": e.detail.value,
        "form.productId": this.data.productList[e.detail.value].value
      });
    },
    onPayTypeChange(e) {
      this.setData({
        "form.payTypeIndex": e.detail.value,
        "form.depositType": this.data.payTypes[e.detail.value].value
      })
    },
    onUpload() {
      if (app.globalData.systemInfo.platform === "iOS") {
        showToast({ msg: "iOS暂不支持上传附件！请至PC端上传" })
      } else {
        dd.navigateTo({
          url: "/pages/upload/upload?accept=.jpg,.png,.jpeg,.pdf&key=firstDepositFile&max=1"
        });
      }
    },
    onRemoveFile(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.data.form.sysEnclosureList.splice(e.target.dataset.index, 1)
            this.setData({
              "form.sysEnclosureList": this.data.form.sysEnclosureList
            })
          }
        },
      });
    }
  },
});

